package game.view.scene
{
	import as3isolib.display.IsoSprite;
	
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import game.controller.GameDispatcher;
	import game.event.EventName;
	import game.event.GameEvent;
	import game.managers.StateManager;
	import game.model.SceneModel;

	public class Employee extends Human
	{
		private var _emSp:IsoSprite;

		private var _path:Array;
		private var _timer:Timer;
		private var _index:int;
		private var _emTween:TweenLite;
		//private var _mc:MovieClip;
		private var _model:SceneModel=SceneModel.getInstance();
		private var emEndPt:Point=new Point(1, 2);
		private var emStartPt:Point=new Point(2, 6);
		private var _payTimer:Timer;
		private var _payFinTimer:Timer;
		/**默认服务员状态
		 * 1-休息
		 * 2-端盘
		 * 3-正常行走
		 * 4-付款
		 * */
		public static const EMPLOYEE_REST:int=1;
		public static const EMPLOYEE_SERV:int=2;
		public static const EMPLOYEE_WALK:int=3;
		public static const EMPLOYEE_PAY:int=4;
		//	private static const EMPLOYEE_PAYWALK:int=4;
		//private var _nowState:int=1;

		private var m:int;
		private var _dispatcher:GameDispatcher=GameDispatcher.getInstance();


		public function Employee()
		{
			_path=[];
			_timer=new Timer(500);
			_payTimer=new Timer(3000);
			_payFinTimer=new Timer(1500);
		}
		
		override public function move(startPt:Point, endPt:Point):void
		{
			emStartPt = startPt;
			emEndPt = endPt;
			
			_path=_model.findPath(startPt, endPt,false);
			
			_timer.start();
			_timer.addEventListener(TimerEvent.TIMER, onEnterFrame);

			m=0;
			_index=0;
		}

		private function onEnterFrame(e:TimerEvent):void
		{
			var targetX:Number=_path[_index].x * 40;
			var targetY:Number=_path[_index].y * 40;

			var dx:Number=targetX - _emSp.x - 160;
			var dy:Number=targetY - _emSp.y - 160;
			var dist:Number=Math.sqrt(dx * dx + dy * dy);
			if (dist < 1)
			{
				_index++;
				checkDir();
				if (_index >= _path.length)
				{
					_timer.removeEventListener(TimerEvent.TIMER, onEnterFrame);
					_timer.stop();
					
					switch (_nowState)
					{
						case 1: /**休息*/
							changeState(_emSp, StateManager.employeeRestRight);
							break;
						case 2: /**端盘*/
							/**emEndPt.y != 5 这个条件是因为固定终点只有2个,2个里面只有第2个终点的Y是5,所以直接用这个判断
							 * 这次的目的地是否是第2终点
							 * */
							if (emEndPt.y != 5) 
							{
								changeState(_emSp, StateManager.employeeServRight);
							}else{
								changeState(_emSp, StateManager.employeeServUp);
							}
							move(emEndPt, emStartPt);
							_nowState=EMPLOYEE_WALK;
							_dispatcher.dispatchEvent(new GameEvent(EventName.SERV_COMPLETE));
							break;
						case 3: /**行走*/
							changeState(_emSp, StateManager.employeeRestRight);
							_nowState=EMPLOYEE_REST;
							
							_payTimer.addEventListener(TimerEvent.TIMER, onPaying);
							_payTimer.start();
							break;
						case 4: /**付款*/
							if (emEndPt.y != 5) 
							{
								changeState(_emSp, StateManager.employeePayRight);
							}else{
								changeState(_emSp, StateManager.employeePayUp);
							}
							//_payTimer=new Timer(1500, 1);
							_payFinTimer.addEventListener(TimerEvent.TIMER, onPayFinish);
							_payFinTimer.start();
							break;
					}
				}
			}
			else
			{
				/**缓动的时间需与timer的时间间隔一致才不会出错或明显的停顿*/
				//	trace("等角坐标" + targetX + "  " + targetY);
				/**每次使用tweenlite都会生成一个新的实例,不控制内存会逐渐加大*/
				_emTween=TweenLite.to(_emSp, 0.5, {x: targetX - 160, y: targetY - 160});
				//	trace(elementList["employee"].x + "," + elementList["employee"].y);
				//_emTween=null;
			}


		}

		private function onPaying(e:TimerEvent):void
		{
			_dispatcher.dispatchEvent(new GameEvent(EventName.CUSTOMER_EATFINISH));
			_nowState=Employee.EMPLOYEE_PAY;

			move(emEndPt,emStartPt );

			_payTimer.removeEventListener(TimerEvent.TIMER, onPaying);
			_payTimer.stop();
			//_payTimer=null;

		}

		private function onPayFinish(e:TimerEvent):void
		{
			_dispatcher.dispatchEvent(new GameEvent(EventName.PAY_FINISH));
			move(emEndPt, emStartPt);
			_nowState=Employee.EMPLOYEE_REST;

			_payFinTimer.removeEventListener(TimerEvent.TIMER, onPaying);
			_payFinTimer.stop();
			//_payTimer=null;
		}

		override protected function checkDir():void
		{
			_mc=_emSp.sprites[0] as MovieClip;
			switch (_model.emDirArr[m])
			{
				case "UP":
					if (_nowState == EMPLOYEE_SERV)
					{
						_mc.gotoAndStop(StateManager.employeeServUp);
						break;
					}
					_mc.gotoAndStop(StateManager.employeeWalkUp);
					break;
				case "DOWN":
					if (_nowState == EMPLOYEE_SERV)
					{
						_mc.gotoAndStop(StateManager.employeeServDown);
						break;
					}
					_mc.gotoAndStop(StateManager.employeeWalkDown);
					break;
				case "LEFT":
					if (_nowState == EMPLOYEE_SERV)
					{
						_mc.gotoAndStop(StateManager.employeeServLeft);
						break;
					}
					_mc.gotoAndStop(StateManager.employeeWalkLeft);
					break;
				case "RIGHT":
					if (_nowState == EMPLOYEE_SERV)
					{
						_mc.gotoAndStop(StateManager.employeeServRight);
						break;
					}
					_mc.gotoAndStop(StateManager.employeeWalkRight);
					break;
			}
			m++;
		}

		public function get emSp():IsoSprite
		{
			return _emSp;
		}

		public function set emSp(value:IsoSprite):void
		{
			_emSp=value;
		}

		override public function dispose():void
		{
			super.dispose();
			_emSp=null;
			_timer=null;
			_emTween=null;
			_payTimer=null;
		}


	}
}